import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

// 图标
import 'font-awesome/css/font-awesome.css'

import { postRequest } from './utils/api'
import { getRequest } from './utils/api'
import { putRequest } from './utils/api'
import { deleteRequest } from './utils/api'
import { getMenus, initRoutes } from './utils/menu'

Vue.config.productionTip = false
Vue.use(ElementUI, {size:'small'})//默认small

//全局插件使用
Vue.prototype.postRequest = postRequest
Vue.prototype.getRequest = getRequest
Vue.prototype.putRequest = putRequest
Vue.prototype.deleteRequest = deleteRequest

router.beforeEach((to, from, next) => {
  let token = window.sessionStorage.getItem('token')
  let user = window.sessionStorage.getItem('user')
  // console.log('token:')
  // console.log(token)
  // console.log('user:')
  // console.log(user)

  if (token) {
    getMenus(router, token)
    if (!user) {
      return getRequest('/user/info').then(resp => {
        if (resp) {
          //存入用户信息
          window.sessionStorage.setItem('user', JSON.stringify(resp.user))
          next();
        }
      })
    }
    next()  //next('/')  next(false) next(error)
  } else {
    // console.log('no token')
    if (to.path == '/') {
      next()
    } else {
      // console.log('redirect')
      next('/?redirect=' + to.path)
    }
  }

})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
